package com.ruoyi.equip.util;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equip.domain.EquipProduct;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.K;

import java.util.HashMap;
import java.util.concurrent.*;

import static com.ruoyi.equip.util.ProductCacheUtils.aliveTimeMap;

public class HeartCacheUtils {



   static ExpiringMap<String, Integer> expiringMap = new ExpiringMap<>();


    /**
     * 设置设备缓存
     */
    public static int getAliveTime(String key) {
        return aliveTimeMap.get(getCacheKey(key));
        //return SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
    }



    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return CacheConstants.PRODUCT_KEY + configKey;
    }


    /**
     * 设置在线缓存
     */

    public static void setOnline(String key, String productKey) {
        int aliveTime = getAliveTime(productKey);
        expiringMap.put("equip:online:" + key,1,aliveTime,TimeUnit.SECONDS);
        //SpringUtils.getBean(RedisCache.class).setCacheObject("equip:online:" + key, 1, aliveTime, TimeUnit.SECONDS);
    }


    /**
     * 判断是否在线
     */
    public static boolean isOnline(String key) {
        //return SpringUtils.getBean(RedisCache.class).hasKey("equip:online:" + key);
        return expiringMap.get("equip:online:" + key)!=null;
    }
}
